package com.aq.appserver.server.account.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.aq.appserver.server.account.bean.AccountBeanPad;
import com.aq.appserver.server.account.entity.Account;

public interface AccountDao extends PagingAndSortingRepository<Account, Long> , JpaSpecificationExecutor<Account>{

	//@Query("select s from Account s where s.phoneNumber = ?1 ")
	Account findByPhoneNumber(String phoneNumber);

	Account findByRegisterCode(String registerCode);
	
	@Query("select new com.aq.appserver.server.account.bean.AccountBeanPad(s.phoneNumber, s.feather, s.nickName, s.registerCode, d.url, d.type, s.speakState)"
			+ " from Account s, HeadImg d where s.phoneNumber = ?1 and s.password = ?2  and s.phoneNumber = d.phoneNumber")
	AccountBeanPad findByPhoneNumberAndPassword(String loginName, String password);

	@Modifying
	@Query("update Account s set s.feather = s.feather + 1 where s.phoneNumber = ?1")
	void updateAccFeather(String phoneNum);

	@Query("select new com.aq.appserver.server.account.bean.AccountBeanPad(s.phoneNumber, s.feather, s.nickName, s.registerCode, d.url, d.type, s.speakState)"
			+ " from Account s, HeadImg d where s.qqUuid = ?1 and s.phoneNumber = d.phoneNumber")
	AccountBeanPad findByQqUuid(String uuid);

	@Query("select new com.aq.appserver.server.account.bean.AccountBeanPad(s.phoneNumber, s.feather, s.nickName, s.registerCode, d.url, d.type, s.speakState)"
			+ " from Account s, HeadImg d where s.wxUuid = ?1 and s.phoneNumber = d.phoneNumber")
	AccountBeanPad findByWxUuid(String uuid);

	@Query("select new com.aq.appserver.server.account.bean.AccountBeanPad(s.phoneNumber, s.feather, s.nickName, s.registerCode, d.url, d.type, s.speakState)"
			+ " from Account s, HeadImg d where s.wbUuid = ?1 and s.phoneNumber = d.phoneNumber")
	AccountBeanPad findByWbUuid(String uuid);

	@Modifying
	@Query("update Account s set s.qqUuid = ?2 where s.phoneNumber = ?1")
	void updateQqUuidByLoginName(String loginName, String uuid);

	@Modifying
	@Query("update Account s set s.wxUuid = ?2 where s.phoneNumber = ?1")
	void updateWxUuidByLoginName(String loginName, String uuid);

	@Modifying
	@Query("update Account s set s.wbUuid = ?2 where s.phoneNumber = ?1")
	void updateWbUuidByLoginName(String loginName, String uuid);

	@Query("select s from Account s where s.nickName like %?1% or s.phoneNumber like %?1%")
	Page<Account> findByNickNameOrPhoneNumber(String accCondition, Pageable pageable);

	List<Account> findByNickNameLikeOrPhoneNumberLike(String nickName, String phoneNumber, Pageable pageable);
	
	@Modifying
	@Query("update Account s set s.speakState = ?2 where s.phoneNumber = ?1")
	void updateSpeakState(String phoneNum, String status);
	
	@Modifying
	@Query("update Account s set s.feather = s.feather + ?2 where s.phoneNumber = ?1")
	void updateAccFeather(String phoneNum, int count);

	@Modifying
	@Query("update Account s set s.nickName = ?2 where s.phoneNumber = ?2")
	void updateNickName(String phoneNumber, String nickName);

	@Query("select new com.aq.appserver.server.account.bean.AccountBeanPad(s.phoneNumber, s.nickName, s.registerCode, d.url, d.type)"
			+ " from Account s, HeadImg d where s.phoneNumber = d.phoneNumber")
	Page<AccountBeanPad> queryAccountInfoList(Pageable pageable);

	@Modifying
	@Query("update Account s set s.password = ?2, s.updateTime = ?3 where s.phoneNumber = ?1")
	void updatePWD(String phoneNumber, String password, Date updateTime);

	@Query("select count(s.id) from Account s where s.nickName = ?1 and s.phoneNumber = ?2")
	int queryAccountListWebCount(String nickName, String phoneNum);
}
